package com.example.ec.controller;

import com.example.ec.model.Commodity;
import com.example.ec.service.CommodityService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("commodity")
public class CommodityController extends BaseController{
    private final CommodityService commodityService;

    public CommodityController(CommodityService commodityService) {
        this.commodityService = commodityService;
    }

    @PostMapping("create")
    private String create(Commodity commodity, MultipartFile file){
        File dir = new File(request.getServletContext().getRealPath("/img"));
        if(!dir.exists()){
            dir.mkdirs();
        }
        String filename = UUID.randomUUID()+"."+ StringUtils.getFilenameExtension(file.getOriginalFilename());
        try {
            file.transferTo(new File(dir,filename));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        commodity.setImg(filename);
        System.out.println(commodity);
        commodityService.create(commodity);
        List<Commodity> commodityList = commodityService.queryAll();
        session.setAttribute("commodityList",commodityList);
        return "redirect:/admin/index.jsp";
    }

    @GetMapping("query/{id}")
    private String queryById(@PathVariable("id") int id){
        Commodity commodity = commodityService.queryOne("queryCommodityById",id);
        session.setAttribute("commodity",commodity);
        return "redirect:/detail.jsp";
    }

    @GetMapping("delete/{id}")
    private String deleteById(@PathVariable("id") int id){
        commodityService.removeById(id);
        List<Commodity> commodityList = commodityService.queryAll();
        session.setAttribute("commodityList",commodityList);
        return "redirect:/admin/index.jsp";
    }

    @GetMapping("alter/{id}")
    private String alterById(@PathVariable("id") int id){
        Commodity commodity = commodityService.queryOne("queryCommodityById",id);
        session.setAttribute("commodity",commodity);
        return "redirect:/admin/edit.jsp";
    }

    @PostMapping("alterById")
    private String alert(Commodity commodity,MultipartFile file){
        File dir = new File(request.getServletContext().getRealPath("/img"));
        if(!dir.exists()){
            dir.mkdirs();
        }
        String filename = UUID.randomUUID() + "." +StringUtils.getFilenameExtension(file.getOriginalFilename());
        try {
            file.transferTo(new File(dir,filename));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        commodity.setImg(filename);
        commodityService.modify("alterCommodityById",commodity);
        List<Commodity> commodityList = commodityService.queryAll();
        session.setAttribute("commodityList",commodityList);
        return "/admin/index.jsp";
    }


    @GetMapping("search")
    private String search(){
        String search = request.getParameter("search");
        List<Commodity> commodityList = commodityService.queryList("queryCommoditysBySearch",search);
        session.setAttribute("commodityList",commodityList);
        return "/user/index.jsp";

    }

}
